//
//  WSMyLikeController.m
//  CharonTVProject
//
//  Created by karong on 2023/8/14.
//

#import "WSMyLikeController.h"
#import "WSMyLikeCell.h"
#import "WSMyLikeReqest.h"
#import "CTMediator+WSPlayerAction.h"

@interface WSMyLikeController () <UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UICollectionView *listView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *editBarBottom;
@property (weak, nonatomic) IBOutlet UIButton *editBtn;
@property (nonatomic, assign) BOOL isEdit;
@property (nonatomic, strong) NSMutableArray * recentlyArray;
@property (nonatomic, strong) NSMutableArray * beforeArray;
@end

@implementation WSMyLikeController

- (void)viewDidLoad {
    [super viewDidLoad];
    WSMyLikeReqest * request = [WSMyLikeReqest new];
    self.request = request;
}

- (BOOL)disableAutoBeginRefresh {
    return YES;
}

- (void)loadSuccessWithArray:(NSArray *)array isRefresh:(BOOL)isRefresh {
    NSMutableArray * rArray = [[NSMutableArray alloc]initWithArray:array];
    NSMutableArray * bArray = [[NSMutableArray alloc]init];
    for (WSVideo * video in rArray) {
        if (video.period == 2) {
            [bArray addObject:video];
        }
    }
    [rArray removeObjectsInArray:bArray];
    if (isRefresh) {
        [self.recentlyArray removeAllObjects];
        self.recentlyArray = [NSMutableArray new];
        [self.beforeArray removeAllObjects];
        self.beforeArray = [NSMutableArray new];
    }
    [self.recentlyArray addObjectsFromArray:rArray];
    [self.beforeArray addObjectsFromArray:bArray];
    [super loadSuccessWithArray:array isRefresh:isRefresh];
}

- (NSInteger)dataArrayCount {
    return self.dataArray.count;
}

- (IBAction)allSelectAction:(UIButton *)sender {
    sender.selected = !sender.selected;
    for (WSVideo * video in self.dataArray) {
        video.isSelected = sender.selected;
    }
    [self.listView reloadData];
}

- (IBAction)editAction:(UIButton *)sender {
    self.isEdit = !self.isEdit;
}

- (IBAction)deleteAction:(id)sender {
    NSMutableArray * delArray = [NSMutableArray new];
    for (WSVideo * video in self.dataArray) {
        if (video.isSelected) {
            [delArray addObject:video];
        }
    }
    if (delArray.count > 0) {
        UIViewController * target = [[CTMediator sharedInstance]ct_showcharonTV_AlterWithTitle:APPLocalized(@"確定要將數據從点赞列表移除嗎？") content:@"" confirm:APPLocalized(@"刪除") confirmHandle:^{
            [self delVideoRequest:delArray];
        } cancel:@""];
        target.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        target.modalPresentationStyle = UIModalPresentationCustom;
        [self presentViewController:target animated:YES completion:nil];
    }
    else {
        self.isEdit = NO;
    }
}

- (void)delVideoRequest:(NSArray*)array {
    [WSToast loading];
    [WSMyLikeReqest delcharonTV_LikeWithChapter_ids:array success:^{
        [WSToast dismiss];
        [self.dataArray removeObjectsInArray:array];
        self.isEdit = NO;
        [self benginRefresh];
    } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {

    }];
}

- (void)initArraySelectStatus {
    for (WSVideo * video in self.dataArray) {
        video.isSelected = NO;
    }
}
#pragma mark UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    NSInteger count = 0;
    if (section == 0) {
        count = self.recentlyArray.count;
    }
    else if (section == 1) {
        count = self.beforeArray.count;
    }
    return count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    WSMyLikeCell * myLikeCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WSMyLikeCell" forIndexPath:indexPath];
    if (indexPath.section == 0) {
        [myLikeCell setDataWithObject:self.recentlyArray[indexPath.row] isEdit:self.isEdit];
    }
    else if (indexPath.section == 1) {
        [myLikeCell setDataWithObject:self.beforeArray[indexPath.row] isEdit:self.isEdit];
    }
    return myLikeCell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize size = CGSizeZero;
    size.width = WSScreenWidth;
    size.height = 158;
    return size;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView * charonTV_likeHeadView;
        if (!charonTV_likeHeadView) {
            charonTV_likeHeadView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"WSLikeHeadView" forIndexPath:indexPath];
            UILabel * label = [charonTV_likeHeadView viewWithTag:101];
            label.text = indexPath.section == 0?APPLocalized(@"一周內"):APPLocalized(@"更早");
        }
        return charonTV_likeHeadView;
    }
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    CGSize size = CGSizeZero;
    size.width = WSScreenWidth;
    if ((self.recentlyArray.count > 0 && section == 0) || (self.beforeArray.count > 0 && section == 1)) {
        size.height = 47;
    }
    return size;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    WSVideo * video;
    if (indexPath.section == 0) {
        video = self.recentlyArray[indexPath.row];
    }
    else if (indexPath.section == 1) {
        video = self.beforeArray[indexPath.row];
    }
    if (self.isEdit) {
        video.isSelected = !video.isSelected;
        [collectionView reloadItemsAtIndexPaths:@[indexPath]];
    }
    else {
        WSVideo * tvideo = [WSVideo new];
        tvideo.id = video.chapter_id;
        tvideo.tv_id = video.tv_id;
        UIViewController * target = [[CTMediator sharedInstance]ct_showcharonTV_PlayerWithVideo:tvideo];
        [self.navigationController pushViewController:target animated:YES];
    }
}

- (void)setIsEdit:(BOOL)isEdit {
    _isEdit = isEdit;
    self.editBarBottom.constant = isEdit?0:-80;
    [self.editBtn setTitle:self.isEdit?APPLocalized(@"取消"):APPLocalized(@"編輯") forState:(UIControlStateNormal)];
    [self.view uploadLayout];
    if (isEdit) {
        [self initArraySelectStatus];
    }
    [self.listView reloadData];
}
@end
